package com.example.demo.controller;

import com.example.demo.dto.recipe.RecipeDto;
import com.example.demo.entity.Dictionary;
import com.example.demo.entity.Recipe;
import com.example.demo.service.IDictionaryService;
import com.example.demo.service.IRecipeService;
import com.example.demo.utils.list.ListUtils;
import com.example.demo.utils.result.Result;
import com.example.demo.utils.result.ResultUtils;
import com.example.demo.vo.recipe.RecipeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 控卡食谱
 */
@RestController
@RequestMapping("recipe")
public class RecipeController extends BaseController {

    @Autowired
    IRecipeService recipeService;

    @Autowired
    IDictionaryService dictionaryService;

    @PostMapping("selectListByDateOrDefault")
    public Result selectListByDateOrDefault(@RequestBody RecipeDto recipeDto){
        Date date = recipeDto.getDate();
        List<Recipe> recipeList = recipeService.selectListByDateOrDefault(date).orElse(new ArrayList<>());
        List<RecipeVo> recipeVoList = ListUtils.copyListProperties(recipeList, RecipeVo.class);
        // 餐别类型转换为对应的中文
        recipeVoList.forEach(t -> t.setTypeCn(
                dictionaryService.selectOneByKey(t.getType()).orElse(new Dictionary()).getValue()
        ));
        // 分组显示
        Map<String, List<RecipeVo>> collect = recipeVoList.stream().collect(Collectors.groupingBy(RecipeVo::getTypeCn));
        return ResultUtils.success(collect);
    }
}
